package com.zhuoyue.biz.core.command.cmd.common.fe6;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.constant.JingYinChuangLian0Fe60209CmdDeal;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.utils.CmdUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 静音窗帘
 *
 * @author Administrator
 */
public class JingYinChuangLian0Fe60209 {

    private static final String OPEN = "OPEN";
    private static final String CLOSE = "CLOSE";
    private static final String STOP = "STOP";

    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String cmd = "";
        switch (controlReq.getControlType()) {
            //开关
            case ControlType.SWITCH_STATE_REQUEST:
                cmd = getSwitchStopCmd(deviceAndGatewayDO, controlReq);
                break;
            //百分比
            case ControlType.PERCENT_REQUEST:
                cmd = getPercentCmd(deviceAndGatewayDO, controlReq);
                break;
            //状态检查
            case ControlType.CHECK_REQUEST:
                cmd = getCheckCmd(deviceAndGatewayDO);
                break;
            case ControlType.TURN_ON_REQUEST:
                Map<String, Object> control = new HashMap<>(1);
                control.put("state", "OPEN");
                controlReq.setActions(control);
                cmd = getSwitchStopCmd(deviceAndGatewayDO, controlReq);
                break;
            case ControlType.TURN_OFF_REQUEST:
                Map<String, Object> control1 = new HashMap<>(1);
                control1.put("state", "CLOSE");
                controlReq.setActions(control1);
                cmd = getSwitchStopCmd(deviceAndGatewayDO, controlReq);
                break;
            case ControlType.PAUSE_REQUEST:
                Map<String, Object> control2 = new HashMap<>(1);
                control2.put("state", "STOP");
                controlReq.setActions(control2);
                cmd = getSwitchStopCmd(deviceAndGatewayDO, controlReq);
                break;
            default:
                break;

        }


        return cmd;
    }


    /**
     * 状态检查
     *
     * @param deviceAndGatewayDO
     * @return
     */
    private static String getCheckCmd(DeviceAndGatewayDO deviceAndGatewayDO) {

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();

        String cmd8 = JingYinChuangLian0Fe60209CmdDeal.CheckCmd.CHECK_CMD;

        String cmd7 = CmdUtils.getDateLength(cmd8);

        String cmd3To8 = JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_3 +
                JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_4 +
                gatewayMac +
                deviceMac +
                cmd7 +
                cmd8;

        String cmd2 = CmdUtils.getDateLength(cmd3To8);

        String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);

        String fullCmd = JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_1 +
                cmd2 +
                cmd3To8 +
                cmd9 +
                JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_10;

        return fullCmd;
    }

    /**
     * 状态检查
     *
     * @param deviceAndGatewayDO
     * @param controlReq
     * @return
     */
    private static String getPercentCmd(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();
        Map<String, Object> actionsMap = controlReq.getActions();

        String value = actionsMap.get("percentage").toString();

        String percentCmd8 = CmdUtils.intToHexTwo(Integer.parseInt(value));

        String percentCmd3To10 = JingYinChuangLian0Fe60209CmdDeal.PercentCmd.CMD_3 +
                JingYinChuangLian0Fe60209CmdDeal.PercentCmd.CMD_4 +
                JingYinChuangLian0Fe60209CmdDeal.PercentCmd.CMD_5 +
                JingYinChuangLian0Fe60209CmdDeal.PercentCmd.CMD_6 +
                JingYinChuangLian0Fe60209CmdDeal.PercentCmd.CMD_7 +
                percentCmd8 +
                JingYinChuangLian0Fe60209CmdDeal.PercentCmd.CMD_9 +
                JingYinChuangLian0Fe60209CmdDeal.PercentCmd.CMD_10;

        String percentCmd2 = CmdUtils.getCmdBodySumCurtains(JingYinChuangLian0Fe60209CmdDeal.PercentCmd.CMD_1 + percentCmd3To10);

        String commonCmd8 = JingYinChuangLian0Fe60209CmdDeal.PercentCmd.CMD_1 +
                percentCmd2 +
                percentCmd3To10;

        String commonCmd7 = CmdUtils.getDateLength(commonCmd8);

        String commonCmd3To8 = JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_3 +
                JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_4 +
                gatewayMac +
                deviceMac +
                commonCmd7 +
                commonCmd8;

        String commonCmd2 = CmdUtils.getDateLength(commonCmd3To8);
        String commonCmd9 = CmdUtils.getCmdBodySum(commonCmd3To8);

        String fullCmd = JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_1 +
                commonCmd2 +
                commonCmd3To8 +
                commonCmd9 +
                JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_10;

        return fullCmd;

    }


    /**
     * 开关停
     *
     * @param deviceAndGatewayDO
     * @param controlReq
     * @return
     */
    private static String getSwitchStopCmd(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();
        Map<String, Object> actionsMap = controlReq.getActions();

        String state = actionsMap.get("state").toString();

        String commonCmd8 = "";
        if (OPEN.equals(state)) {

            commonCmd8 = JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_1 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_2_OPEN +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_3 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_4 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_5 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_6 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_7_OPEN +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_8 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_9 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_10;

        } else if (CLOSE.equals(state)) {

            commonCmd8 = JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_1 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_2_CLOSE +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_3 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_4 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_5 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_6 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_7_CLOSE +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_8 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_9 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_10;

        } else if (STOP.equals(state)) {

            commonCmd8 = JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_1 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_2_STOP +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_3 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_4 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_5 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_6 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_7_STOP +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_8 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_9 +
                    JingYinChuangLian0Fe60209CmdDeal.SwitchStopCmd.CMD_10;

        }

        String commonCmd7 = CmdUtils.getDateLength(commonCmd8);

        String commonCmd3To8 = JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_3 +
                JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_4 +
                gatewayMac +
                deviceMac +
                commonCmd7 +
                commonCmd8;

        String commonCmd2 = CmdUtils.getDateLength(commonCmd3To8);
        String commonCmd9 = CmdUtils.getCmdBodySum(commonCmd3To8);

        String fullCmd = JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_1 +
                commonCmd2 +
                commonCmd3To8 +
                commonCmd9 +
                JingYinChuangLian0Fe60209CmdDeal.CommonCmd.CMD_10;

        return fullCmd;
    }

}
